<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="创建时间" prop="createDate">
      <el-input v-model="dataForm.createDate" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="modifyDate">
      <el-input v-model="dataForm.modifyDate" placeholder="修改时间"></el-input>
    </el-form-item>
    <el-form-item label="账号ID" prop="accountId">
      <el-input v-model="dataForm.accountId" placeholder="账号ID"></el-input>
    </el-form-item>
    <el-form-item label="设备号" prop="device">
      <el-input v-model="dataForm.device" placeholder="设备号"></el-input>
    </el-form-item>
    <el-form-item label="应用ID" prop="ip">
      <el-input v-model="dataForm.ip" placeholder="应用ID"></el-input>
    </el-form-item>
    <el-form-item label="签名的APPID" prop="appId">
      <el-input v-model="dataForm.appId" placeholder="签名的APPID"></el-input>
    </el-form-item>
    <el-form-item label="下载地址" prop="downloadUrl">
      <el-input v-model="dataForm.downloadUrl" placeholder="下载地址"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          createDate: '',
          modifyDate: '',
          accountId: '',
          device: '',
          ip: '',
          appId: '',
          downloadUrl: ''
        },
        dataRule: {
          createDate: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          modifyDate: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          accountId: [
            { required: true, message: '账号ID不能为空', trigger: 'blur' }
          ],
          device: [
            { required: true, message: '设备号不能为空', trigger: 'blur' }
          ],
          ip: [
            { required: true, message: '应用ID不能为空', trigger: 'blur' }
          ],
          appId: [
            { required: true, message: '签名的APPID不能为空', trigger: 'blur' }
          ],
          downloadUrl: [
            { required: true, message: '下载地址不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/st/stsign/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.createDate = data.stSign.createDate
                this.dataForm.modifyDate = data.stSign.modifyDate
                this.dataForm.accountId = data.stSign.accountId
                this.dataForm.device = data.stSign.device
                this.dataForm.ip = data.stSign.ip
                this.dataForm.appId = data.stSign.appId
                this.dataForm.downloadUrl = data.stSign.downloadUrl
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/st/stsign/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'createDate': this.dataForm.createDate,
                'modifyDate': this.dataForm.modifyDate,
                'accountId': this.dataForm.accountId,
                'device': this.dataForm.device,
                'ip': this.dataForm.ip,
                'appId': this.dataForm.appId,
                'downloadUrl': this.dataForm.downloadUrl
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
